const mongoose = require('mongoose');

// 步骤进度模式
const stepProgressSchema = new mongoose.Schema({
  stepId: {
    type: mongoose.Schema.Types.ObjectId,
    required: true
  },
  status: {
    type: String,
    enum: ['not_started', 'in_progress', 'completed'],
    default: 'not_started'
  },
  startedAt: {
    type: Date
  },
  completedAt: {
    type: Date
  },
  timeSpent: {
    type: Number, // 实际花费时间（分钟）
    default: 0
  }
});

// 阶段进度模式
const stageProgressSchema = new mongoose.Schema({
  stageId: {
    type: mongoose.Schema.Types.ObjectId,
    required: true
  },
  status: {
    type: String,
    enum: ['not_started', 'in_progress', 'completed'],
    default: 'not_started'
  },
  startedAt: {
    type: Date
  },
  completedAt: {
    type: Date
  },
  stepsProgress: [stepProgressSchema]
});

// 用户学习进度模式
const userLearningProgressSchema = new mongoose.Schema({
  userId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true
  },
  learningPathId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'LearningPath',
    required: true
  },
  status: {
    type: String,
    enum: ['not_started', 'in_progress', 'completed'],
    default: 'not_started'
  },
  currentStageIndex: {
    type: Number,
    default: 0
  },
  currentStepIndex: {
    type: Number,
    default: 0
  },
  progress: {
    type: Number, // 总体进度百分比
    default: 0
  },
  startedAt: {
    type: Date
  },
  completedAt: {
    type: Date
  },
  lastAccessedAt: {
    type: Date,
    default: Date.now
  },
  stagesProgress: [stageProgressSchema]
});

// 索引优化查询
userLearningProgressSchema.index({ userId: 1, learningPathId: 1 }, { unique: true });

const UserLearningProgress = mongoose.model('UserLearningProgress', userLearningProgressSchema);

module.exports = UserLearningProgress; 